package com.weiqiao.report.distributior.rest;

import com.mongodb.client.model.Filters;
import com.weiqiao.mall.domain.AccountDetailEntity;
import com.weiqiao.mall.service.IAccountDetailService;
import com.weiqiao.modules.utils.BaseServlet;
import com.weiqiao.report.distributior.Service.DistributiorService;
import com.weiqiao.report.util.RequestCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/distributior")
public class DistributiorController extends BaseServlet {

    @Autowired
    @Qualifier("DistributiorServiceImpl")
    private DistributiorService distributiorService;

    @Autowired
    @Qualifier("AccountDetailServiceImpl")
    private IAccountDetailService accountDetailService;

    @ResponseBody
    @RequestMapping("/exportAccountedDetailList")
    public ResponseEntity exportAccountedDetailList(HttpServletRequest request, HttpServletResponse response) {
        return distributiorService.exportAccountedDetailList(request, response);
    }

    @ResponseBody
    @RequestMapping("/accountedDetail")
    public ResponseEntity accountedDetail(HttpServletRequest request) {
        RequestCheckUtil.checkCompanyid(request);
        int company_id = Integer.valueOf(request.getParameter("company_id"));
        Bson bson = Filters.eq("delete_flag", 0);
        int pageSize = RequestCheckUtil.checkPageSize(request);
        int pageNo = RequestCheckUtil.checkPageNo(request);
        bson = distributiorService.getAccountedDetailQueryParamter(request, bson);
        Bson sort = Filters.eq("creat_time", -1);
        int recordCount = accountDetailService.countNum(company_id, bson);
        List<AccountDetailEntity> account_detail_list =
                accountDetailService.queryAccountDetail(bson, company_id, sort, pageNo, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("content", account_detail_list);
        map.put("totalElements", recordCount);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/distributiorStatistics")
    public ResponseEntity distributiorStatistics(HttpServletRequest request) {
        RequestCheckUtil.checkCompanyid(request);
        // 目前先以分销员进行分页
        Map map = new HashedMap();
        return new ResponseEntity(map, HttpStatus.OK);
    }


    @ResponseBody
    @RequestMapping("/exportDistributiorStatistics")
    public ResponseEntity exportDistributiorStatistics(HttpServletRequest request, HttpServletResponse response) {
        return distributiorService.exportDistributiorStatistics(request, response);
    }

    @ResponseBody
    @RequestMapping("/rebaterList")
    public ResponseEntity rebaterList(HttpServletRequest request) {
        return new ResponseEntity(new HashMap<>(), HttpStatus.OK);
    }
}
